/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.liangjun.antibodydetectionproject.service.impl;

import me.liangjun.antibodydetectionproject.domain.AntibodyDetectionProject;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.liangjun.antibodydetectionproject.repository.AntibodyDetectionProjectRepository;
import me.liangjun.antibodydetectionproject.service.AntibodyDetectionProjectService;
import me.liangjun.antibodydetectionproject.service.dto.AntibodyDetectionProjectDto;
import me.liangjun.antibodydetectionproject.service.dto.AntibodyDetectionProjectQueryCriteria;
import me.liangjun.antibodydetectionproject.service.mapstruct.AntibodyDetectionProjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wei
* @date 2021-09-04
**/
@Service
@RequiredArgsConstructor
public class AntibodyDetectionProjectServiceImpl implements AntibodyDetectionProjectService {

    private final AntibodyDetectionProjectRepository antibodyDetectionProjectRepository;
    private final AntibodyDetectionProjectMapper antibodyDetectionProjectMapper;

    @Override
    public Map<String,Object> queryAll(AntibodyDetectionProjectQueryCriteria criteria, Pageable pageable){
        Page<AntibodyDetectionProject> page = antibodyDetectionProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(antibodyDetectionProjectMapper::toDto));
    }

    @Override
    public List<AntibodyDetectionProjectDto> queryAll(AntibodyDetectionProjectQueryCriteria criteria){
        return antibodyDetectionProjectMapper.toDto(antibodyDetectionProjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AntibodyDetectionProjectDto findById(Long id) {
        AntibodyDetectionProject antibodyDetectionProject = antibodyDetectionProjectRepository.findById(id).orElseGet(AntibodyDetectionProject::new);
        ValidationUtil.isNull(antibodyDetectionProject.getId(),"AntibodyDetectionProject","id",id);
        return antibodyDetectionProjectMapper.toDto(antibodyDetectionProject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AntibodyDetectionProjectDto create(AntibodyDetectionProject resources) {
        return antibodyDetectionProjectMapper.toDto(antibodyDetectionProjectRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AntibodyDetectionProject resources) {
        AntibodyDetectionProject antibodyDetectionProject = antibodyDetectionProjectRepository.findById(resources.getId()).orElseGet(AntibodyDetectionProject::new);
        ValidationUtil.isNull( antibodyDetectionProject.getId(),"AntibodyDetectionProject","id",resources.getId());
        antibodyDetectionProject.copy(resources);
        antibodyDetectionProjectRepository.save(antibodyDetectionProject);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            antibodyDetectionProjectRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AntibodyDetectionProjectDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AntibodyDetectionProjectDto antibodyDetectionProject : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("pID", antibodyDetectionProject.getPId());
            map.put("抗体检测ID", antibodyDetectionProject.getAId());
            map.put("检测时间", antibodyDetectionProject.getAntibodyType());
            map.put("结果", antibodyDetectionProject.getAntibodyResult());
            map.put("检查方法", antibodyDetectionProject.getAntibodyMethod());
            map.put("数值结果", antibodyDetectionProject.getAntibodyNum());
            map.put("图片", antibodyDetectionProject.getPicture());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}